package com.fc.app.fcapp.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.fc.library.fresco.Util;

import java.util.List;

/**
 * Created by fc on 2018/5/7.
 * <mPaint>
 * 根据坐标系list来绘制折线
 */

public class PathLine extends View {
    private Paint mPaint;
    private Path mPath;
    private List<Point> mPointList;

    public PathLine(Context context) {
        super(context);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3.0f);

        mPath = new Path();
    }

    public void setPainColor(int color) {
        if (mPaint != null) {
            mPaint.setColor(color);
        }
    }

    public void setPointList(List<Point> pointList) {
        mPointList = pointList;
        if (mPointList != null && mPointList.size() > 0) {
            mPath.moveTo(Util.Div(mPointList.get(0).getX()), Util.Div(mPointList.get(0).getY()));
            for (int i = 1; i < mPointList.size(); i++) {
                mPath.lineTo(Util.Div(mPointList.get(i).getX()), Util.Div(mPointList.get(i).getY()));
            }
        }
    }

    Point mLastPoint;

    public void setPoint(Point p, boolean isFirstPoint) {
        if (p != null) {
            if (isFirstPoint) {
                mLastPoint = p;
            }
            mPath.moveTo(Util.Div(mLastPoint.getX()), Util.Div(mLastPoint.getY()));
            if (!isFirstPoint)
                mPath.lineTo(Util.Div(p.getX()), Util.Div(p.getY()));
            mLastPoint = p;
            invalidate();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath, mPaint);
    }
}

